<template>
  <div class="diamond">
    <div class="diamond-scroll-wrapper">
      <div ref="scroll" class="diamond-scroll">
        <ul class="diamondlist" :class="{ 'items-10': items.length <= 10 }">
          <li
            v-for="(item, index) in items"
            :key="index"
            class="diamondlist__item"
            @click="handleClick(item, index)"
          >
            <img class="item__img" :src="item.imgUrl" />
            <span class="item__tit"> {{ item.imgName || '' }}</span>
          </li>
        </ul>
      </div>
    </div>
    <div class="scrollbar-track" :class="{ visible: items.length > 10 }">
      <div
        class="scrollbar-thumb"
        :style="{ transform: `translateX(${rate})` }"
      ></div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'diamond-list',
  props: {
    items: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      maxScrollX: 0,
      rate: 0
    };
  },
  mounted() {
    this.init();
  },
  beforeDestroy() {
    this.destroy();
  },
  methods: {
    getMaxScrollX() {
      const $scroll = this.$refs.scroll;
      if ($scroll) {
        this.maxScrollX = $scroll.scrollWidth - $scroll.clientWidth;
      }
    },
    handleClick(...params) {
      this.$emit('click', ...params);
    },
    handleScroll(e) {
      const currentX = e.target.scrollLeft;
      let rate = Number((currentX / this.maxScrollX) * 100);
      if (rate < 0) {
        rate = 0;
      }
      if (rate > 50) {
        rate = 50;
      }
      this.rate = `${rate.toFixed(2)}%`;
    },
    init() {
      const $scroll = this.$refs.scroll;
      if ($scroll) {
        this.getMaxScrollX();
        $scroll.addEventListener('scroll', this.handleScroll);
      }
    },
    destroy() {
      const $scroll = this.$refs.scroll;
      if ($scroll) {
        $scroll.removeEventListener('scroll', this.handleScroll);
      }
    }
  }
};
</script>

<style lang="scss" scoped>
.diamond {
  background: #ffffff;
  position: relative;
  width: 100%;
  padding-top: 10px;
  overflow: hidden;
  &::before {
    content: '';
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;
    height: 22px;
    background-image: linear-gradient(#ffffff, #f5f5f5);
  }
}

.diamond-scroll-wrapper {
  height: 148px;
  overflow: hidden;
}
.diamond-scroll {
  overflow-x: scroll;
  overflow-y: hidden;
  padding-bottom: 10px;
  margin-bottom: -10px;
  // &::-webkit-scrollbar {
  //   height: 0;
  //   width: 0;
  //   color: transparent;
  // }
}

.scrollbar-track {
  margin: 0 auto 10px;
  position: relative;
  width: 30px;
  height: 2px;
  background: rgba(204, 204, 204, 1);
  border-radius: 2px;
  visibility: hidden;

  &.visible {
    visibility: visible;
  }

  .scrollbar-thumb {
    position: absolute;
    top: 0;
    left: 0;
    width: 20px;
    height: 2px;
    background: rgba(255, 16, 16, 1);
    border-radius: 2px;
    z-index: 100;
    transition: all 0.1s linear;
    transform-origin: right;
  }
}

.diamondlist {
  height: 148px;
  display: flex;
  flex-flow: column;
  flex-wrap: wrap;
  overflow: visible;

  &.items-10 .diamondlist__item {
    width: 20%;
  }

  .diamondlist__item {
    margin-bottom: 10px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    min-width: 44px;
    width: 18%;

    .item__img {
      width: 44px;
      height: 44px;
    }

    .item__tit {
      margin-top: 4px;
      height: 16px;
      font-size: 13px;
      font-family: PingFangSC-Regular, PingFang SC;
      font-weight: 400;
      color: rgba(51, 51, 51, 1);
      line-height: 16px;
      text-align: center;
    }
  }
}
</style>
